<template>
    <div>
        <Modal v-model="isShowModal" :title="title" @on-cancel="hide('formValidate')" :mask-closable="false">

            <Form ref="formValidate" :model="params" :rules="ruleValidate" label-position="top">
                <Alert show-icon closable>
                    1.“变动资金”输入正数是增加资金，输入负数是减少资金，余额请保留两位小数，知豆请输入整数;<br>
                    2.“变动描述”输入之后会展示到用户的资金变动里面，不填写展示默认的；
                </Alert>
                <FormItem label="资金类型：" prop="capitalType">
                    <RadioGroup v-model="params.capitalType">
                        <Radio label="1" border>余额</Radio>
                        <Radio label="2" border>知豆</Radio>
                    </RadioGroup>
                </FormItem>
                <FormItem label="变动资金：" prop="updateAmount">
                    <Input v-model.trim="params.updateAmount" type="number" :placeholder="amountDes"></Input>
                </FormItem>
                <FormItem label="变动描述：" prop="paramTitle">
                    <Input v-model.trim="params.paramTitle" type="textarea" placeholder="请输入变动描述"></Input>
                </FormItem>
            </Form>

            <!-- 重写对话框底部按钮 -->
            <div slot="footer">
                <Button @click="hide('formValidate')">取消</Button>
                <Button v-if="type != 1" type="primary" @click="handleSubmit('formValidate')" :loading="loadingBtn">
                    <span v-if="!loadingBtn">确定</span>
                    <span v-else>请稍候...</span>
                </Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                isShowModal: false,
                loadingBtn: false,
                title: '查看信息',
                type: 0,
                params: {
                    userId: '',
                    paramTitle: '',
                    updateAmount: '',
                    capitalType: '1'
                },
                ruleValidate: {
                    updateAmount: [
                        {required: true, message: '请输入变动资金', trigger: 'change'}
                    ]
                },
                amountDes:''
            }
        },
        methods: {
            // 点击提交验证
            handleSubmit(name) {
                // console.log(this.params);
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        // this.$Message.error('验证通过');
                        /**
                         * type判断
                         * 1.查看 2.新增 3.编辑
                         */
                        switch (this.type) {
                            case 1:
                                // this.title = '查看资金';
                                break;
                            case 2:
                                // 新增资金
                                // this.addAdmin();
                                break;
                            case 3:
                                // 编辑资金
                                this.updateAmount();
                                break;
                            default:
                                this.$Notice.error({
                                    title: '资金管理',
                                    desc: '查看，新增，编辑区分判断报错',
                                    duration: 0
                                });
                                break
                        }
                    } else {
                        this.$Message.error('还有信息没有填写合格哦~');
                    }
                })
            },
            // 显示模态框
            show(row, type) {
                this.isShowModal = true;
                this.type = type;
                /**
                 * type判断
                 * 1.查看 2.新增 3.编辑
                 */
                switch (type) {
                    case 1:
                        this.title = '查看资金';
                        // this.findAdminUserById(row);
                        break;
                    case 2:
                        this.title = '新增资金';
                        break;
                    case 3:
                        this.title = '编辑用户资金';
                        // this.findAdminUserById(row);
                        console.log(row)
                        this.params.userId = row.userId;
                        this.amountDes = `当前用户余额：${row.amount}，知豆：${row.integral}`;
                        break;
                    default:
                        this.$Notice.error({
                            title: '资金管理',
                            desc: '查看，新增，编辑模态框报错',
                            duration: 0
                        });
                        break
                }
            },
            // 编辑资金
            updateAmount(){
                this.$Spin.show();
                this.axios.post('/user/updateAmount', this.params).then(res => {
                    
                    setTimeout(()=>{
                        this.$Spin.hide();
                    },300);
                    if (res.status == 200) {
                        this.$emit('changeDate');
                        this.$Message.success('操作成功.');
                        this.hide('formValidate');
                    }
                    if(res.status == 201){
                        this.$Message.error(res.msg);
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '编辑资金接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 关闭模态框
            hide(name) {
                this.isShowModal = false;
                this.$refs[name].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>